package com.groupbuying.customer.service.impl;

import com.groupbuying.customer.pojo.CustomerInvitationCodeEnum;
import com.groupbuying.customer.utils.DateFormatHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.groupbuying.customer.dao.CustomerInvitationCodeDao;
import com.groupbuying.customer.domain.CustomerInvitationCodeDO;
import com.groupbuying.customer.service.CustomerInvitationCodeService;



@Service
public class CustomerInvitationCodeServiceImpl implements CustomerInvitationCodeService {
	@Autowired
	private CustomerInvitationCodeDao customerInvitationCodeDao;
	
	@Override
	public CustomerInvitationCodeDO get(String uuid){
		return customerInvitationCodeDao.get(uuid);
	}
	
	@Override
	public List<CustomerInvitationCodeDO> list(Map<String, Object> map){
		return customerInvitationCodeDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return customerInvitationCodeDao.count(map);
	}
	
	@Override
	public int save(CustomerInvitationCodeDO invitationCode){
		return customerInvitationCodeDao.save(invitationCode);
	}
	
	@Override
	public int update(CustomerInvitationCodeDO invitationCode){
		return customerInvitationCodeDao.update(invitationCode);
	}
	
	@Override
	public int remove(String uuid){
		return customerInvitationCodeDao.remove(uuid);
	}
	
	@Override
	public int batchRemove(String[] uuids){
		return customerInvitationCodeDao.batchRemove(uuids);
	}

    @Override
    public void saveCustomerCodeRelation(String useCustomerUuid, String customerName, String invitationCode) {
		if(!StringUtils.isEmpty(useCustomerUuid)&&!StringUtils.isEmpty(invitationCode)){
			CustomerInvitationCodeDO code= this.getByInvitationCode(invitationCode);
			if(code!=null){
				code.setUsecustomeruuid(useCustomerUuid);
				code.setState(CustomerInvitationCodeEnum.USED);
				//注册使用邀请码时间
				code.setCreatecustomertime(DateFormatHelper.getNowTimeStr());
				this.update(code);

			/*	CInvitationCodeLogModel logModel=new CInvitationCodeLogModel();
				logModel.setOperCustomerUuid(useCustomerUuid);
				logModel.setOperationTime(DateFormatHelper.getNowTimeStr());
				logModel.setInvitationCodeUuid(code.getUuid());
				String remaks=CUSTOMER_NAME+customerName+CUSTOMER_REGISTER_CODE+invitationCode;
				logModel.setRemaks(remaks);
				codeLogService.create(logModel);*/
			}
		}
    }

	@Override
	public CustomerInvitationCodeDO getByInvitationCode(String invitationCode) {
		Map map = new HashMap();
		map.put("invitationcode",invitationCode);
		map.put("delflag","1");
		List<CustomerInvitationCodeDO> list = customerInvitationCodeDao.list(map);
		if(list !=null){
			return list.size()>0 ? list.get(0):null;
		}
		return null;
	}

}
